//
//  DataViewController.m
//  GameBoss
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DataViewController.h"
#import "EnterState.h"
#import "SettingViewController.h"
@interface DataViewController ()<UIWebViewDelegate>
@property (nonatomic, retain)UIWebView *webView;
@property (nonatomic, retain)UIActivityIndicatorView *activityIndicator;
@end

@implementation DataViewController
- (void)dealloc
{
    [_activityIndicator release];
    [_webView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height - 64)];
    [self.view addSubview:self.webView];
    self.webView.delegate= self;
    self.title = self.name;
    NSString *strURL = @"";
    if (self.art.modelId.length != 0) {
        strURL = [NSString stringWithFormat:@"http://mrobot.pcgames.com.cn/v3/cms/articles/%@?size=16&template=(null)", self.art.modelId];
    }
    if (self.focus.id.length != 0) {
        strURL = [NSString stringWithFormat:@"http://mrobot.pcgames.com.cn/v3/cms/articles/%@?size=16&template=(null)", self.focus.id];
    }

//            strURL = [NSString stringWithFormat:@"http://mrobot.pcgames.com.cn/v3/cms/articles/%@?size=16&template=(null)", self.art.modelId];
    NSURL *url =[NSURL URLWithString:strURL];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickLeft)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"equipment"] style:(UIBarButtonItemStylePlain) target:self action:@selector(clickRight)];
    
    self.webView.scalesPageToFit = YES;
    [_webView release];
    
#pragma mark 加载动画
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    self.activityIndicator.center = self.view.center;
    self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.view addSubview:self.activityIndicator];
    
    [self.activityIndicator startAnimating];
    
}
- (void)clickLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)clickRight
{
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        NSString *dataPath = [sandBoxPath stringByAppendingPathComponent:@"collect.plist"];
        //反归档
        NSMutableArray *arr =[NSKeyedUnarchiver unarchiveObjectWithFile:dataPath];
        //判断是否收藏, 收藏是YES
        BOOL ifCollect = NO;
    if (self.art.modelId.length != 0) {
        for (ArticleListModel *article in arr) {
            if ( [article.title isEqualToString:self.name]) {
                ifCollect = YES;
            }
        }
        if (ifCollect == YES) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }else
        {
            NSMutableArray *collectArr = [NSMutableArray arrayWithArray:arr];
            [collectArr addObject:self.art];
            [NSKeyedArchiver archiveRootObject:collectArr toFile:dataPath];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
    }
    if (self.focus.id.length != 0) {
        for (FocusModel *focus in arr) {
            if ( [focus.title isEqualToString:self.name]) {
                ifCollect = YES;
            }
        }
        if (ifCollect == YES) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }else
        {
            NSMutableArray *collectArr = [NSMutableArray arrayWithArray:arr];
            ////////*//
            [collectArr addObject:self.focus];
            [NSKeyedArchiver archiveRootObject:collectArr toFile:dataPath];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }

        
    }
}
//确认
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        SettingViewController *settingVC = [[SettingViewController alloc] init];
        [self.navigationController pushViewController:settingVC animated:YES];
        [settingVC release];
    }
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
#pragma mark 修改webView的颜色只能在这里写
    if ([NightMode shareNightMode].isNight) {
        [webView stringByEvaluatingJavaScriptFromString:@" document.body.style.background = '#2E2E2E'"];
         [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
    } else {
        [webView stringByEvaluatingJavaScriptFromString:@" document.body.style.background = 'white' "];
         [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#2E2E2E'"];
    }
    
    [self.activityIndicator stopAnimating];

}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.activityIndicator stopAnimating];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 选取白黑两种模式
- (void)setNightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    //    [NightManager setLabelColorWithLabel:self.title];
    [NightManager setBackgroundColorWithWebView:self.webView];
    
}

- (void)setLightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    //    [NightManager setLabelColorWithLabel:self.title];
    [NightManager setBackgroundColorWithWebView:self.webView];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
